package com.lcq.study.utils;

import com.alibaba.fastjson.JSON;

import java.io.*;
import java.util.List;

/**
 * @ClassName FolderTemplateSqlUtils
 * @Description
 * @Author lichuanqi
 * @Date 2022/7/8 13:40
 * @Version 1.0
 **/
public class FolderTemplateSqlUtils {
    public static void main(String[] args) throws IOException {

        String templateParentId = "folder0000000001";
        String parentId = "1000000000001";
        long folderIdStart = 1000000000003L;
        int templateFolderIdStart = 3;
        String rootId = "1000000000001";

        String jsonStr = read();
        List<ItemDto> itemDtos = JSON.parseArray(jsonStr, ItemDto.class);
        BufferedWriter writer = new BufferedWriter(new FileWriter("D:/test/folder-template/output"+System.currentTimeMillis()+".txt"));
        ScanDataDto scanDto = new ScanDataDto();
        scanDto.setFolderID(folderIdStart);
        scanDto.setTemplateFolderID(templateFolderIdStart);
        scanItem(itemDtos, writer, scanDto, templateParentId, parentId, rootId,2);

        writer.close();
    }

    private static void scanItem(List<ItemDto> itemDtos,
                                 BufferedWriter writer,
                                 ScanDataDto scanDto,
                                 String templateParentId,
                                 String parentId,
                                 String rootId,
                                 int folderType) throws IOException {

        if (itemDtos==null || itemDtos.size()==0){
            return;
        }

        for (int i = 0; i < itemDtos.size(); i++) {
            long folderID = scanDto.getFolderID();
            int templateFolderID = scanDto.getTemplateFolderID();
            ItemDto itemDto = itemDtos.get(i);
            String name = itemDto.getName();
            PubFolderTemplateDto dto = new PubFolderTemplateDto();
            String fId = String.valueOf(folderID++);
            dto.setFolderId(fId);
            dto.setFolderType(folderType);
            dto.setFolderName(name);
            String tfId = String.format("folder%010d", templateFolderID++);
            dto.setTemplateFolderID(tfId);
            dto.setTemplateParentID(templateParentId);
            dto.setRootID(rootId);
            dto.setParentID(parentId);
            dto.setDataOrder(i+1);
            String sql = getSql(dto);
            writer.write(sql);
            writer.newLine();
            scanDto.setFolderID(folderID);
            scanDto.setTemplateFolderID(templateFolderID);
            scanItem(itemDto.getSubItems(), writer, scanDto, tfId, fId, "1000000000001",3);
        }
    }

    private static String getSql(PubFolderTemplateDto dto) {
        String templateSql = "INSERT INTO PubFolderTemplate(FolderID, FolderName, ParentID, DataOrder, RootID, FolderType, TemplateFolderID, TemplateParentID) VALUES ('%s', N'%s', '%s', %d, '%s', %d, '%s', '%s');";
        return String.format(templateSql, dto.getFolderId(), dto.getFolderName(), dto.getParentID(),
                dto.getDataOrder(), dto.getRootID(), dto.getFolderType(), dto.getTemplateFolderID(), dto.getTemplateParentID());
    }

    private static String read() throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new FileReader("D:/test/folder-template/input20220826.txt"));
        String line = "";
        StringBuilder stringBuilder = new StringBuilder();
        while((line = bufferedReader.readLine())!=null){
            stringBuilder.append(line);
        }
        bufferedReader.close();
        return stringBuilder.toString();
    }
}
